import type {
  CalcAmountInfo,
  ChargeFrameItem,
  ChargeRuleTableItem,
} from '@/pages/ChargeRule/types.d';

const checkSmallLarge = (data: CalcAmountInfo) => {
  if (!data) return false;
  if (!data.freeTimes?.length && !data.chargeFrames?.length) return false;
  return true;
};

export const mapChargeRule = (chargeRule: ChargeRuleTableItem) => {
  if (!chargeRule)
    return {
      dayMax: 0,
      freeMin: 0,
      freeTimes: [0, 0],
      chargeFrames: [],
    };
  const { commonSmall } = chargeRule;
  if (!checkSmallLarge(commonSmall)) {
    const tempFrame: ChargeFrameItem[] = [
      { start: 1, end: 1, unit: 60, price: chargeRule.firstHour || 0 },
    ];
    const mapFrame = chargeRule.chargeFrames?.map((item) => {
      if ((item.start = 1)) {
        item.start = 2;
      }
      if ((item.end = 24)) {
        item.end = 23;
      }
      return item;
    });

    return {
      dayMax: chargeRule.dayMax,
      freeMin: chargeRule.freeMin,
      freeTimes: chargeRule.freeTimes,
      chargeFrames: tempFrame.concat(mapFrame),
    };
  }
  return commonSmall;
};
